﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuceneWrap.Lib;
using LuceneWrap.Web.Common;
using NUnit.Framework;

namespace LuceneWrap.Test
{
    [TestFixture]
    public class LuceneManagerFixture
    {
        
            
        [SetUp]
        public void InitTest()
        {
        }

        LuceneManager<FeedResult> _luceneManager = new LuceneManager<FeedResult>();
        LuceneSearcher _luceneSearcher = new LuceneSearcher();

        [Test]
        public void WriteIndex_Test()
        {
            //We retrieve a list of feeds from a website and get a list of FeedResult
            List<FeedResult> feeds = FeedManager.GetFeeds();
            foreach (var feed in feeds)
            {
                _luceneManager.AddItemToIndex(feed);    
            }
            _luceneManager.FinalizeWriter(true);
            
        }

        [Test]
        public void SearchInIndex_Test()
        {
            //we retrieve a list of FeedResult by searching on the Summary field any occurence of "presentations"
            var result = _luceneSearcher.Search<FeedResult>("Summary", "presentation");
            foreach (var feedResult in result)
            {
                Console.WriteLine(feedResult.Id);
                Console.WriteLine(feedResult.Title);
                Console.WriteLine(feedResult.Summary);
                Console.WriteLine(Environment.NewLine);
            }
        }

[Test]
public void UpdateIndex_Test()
{
    var feeds = FeedManager.GetFeeds();
    var oldFeed = feeds.First();
            
    FeedResult newField = new FeedResult(){Id = oldFeed.Id, Summary = "CIAO CIAO",Title = oldFeed.Title};
    _luceneManager.ModifyItemFromIndex(oldFeed,newField);
    _luceneManager.FinalizeWriter(true);
}

[Test]
public void SearchModifiedEntry_Test()
{
    //we retrieve a list of FeedResult by searching on the Summary field any occurence of "presentations"
    var result = _luceneSearcher.Search<FeedResult>("Summary", "CIAO CIAO");
    foreach (var feedResult in result)
    {
        Console.WriteLine(feedResult.Id);
        Console.WriteLine(feedResult.Title);
        Console.WriteLine(feedResult.Summary);
        Console.WriteLine(Environment.NewLine);
    }
}

    }
}
